

let express=require('express')
let fs=require('fs')
let path=require('path')
let router=express.Router()

//router.get/post..等是精准匹配
router.get('/',(req,res)=>{
    res.cookie('name','张三',{maxAge:60*60*1000})
    res.send('hello')
})

router.get('/api',(req,res)=>{
    console.log(req.session,res.session)
    req.session.age='1000'
    res.send('name')
})

router.get('/home',(req,res)=>{
    //获取传参
    let query=req.query
    console.log(query)
    let aa=req.session.age
    console.log(aa)
    //会根据send()的内容 转换成相应的content-TYPE
    res.send('query')
})

router.get('/pid/:id',(req,res)=>{
    let id=req.params['id']
    let query=req.query
    let body={
        id,
        query
    }
    res.status(200).send(body)
})

router.get('/html',(req,res)=>{
   let file=fs.readFileSync(path.join(__dirname,'a.html'),'utf-8')
//    res.writeHead(200,{
//        'content-type':"text/html"
//    })
   res.send(file)
})

router.post('/post',(req,res)=>{
    let query=req.body


    console.log(query,'post')
    res.send(query)
})

router.get('/list',(req,res)=>{
    let data=[1,5,3,9]
    res.set({'content-type':'application/json'})
    //会自动去list文件下找模板数据配置
    res.render('list',{
        datalist:JSON.stringify(data)
    })
    res.send()
})

module.exports=router